﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using Saxon.Api;

namespace WSManager.Logic.Transformation
{
    static class XsltTransformerFactory
    {
        static readonly Processor XsltProc = new Processor();
        static XsltCompiler _xsltComp;
        static XsltCompiler XsltComp
        {
            get { return _xsltComp ?? (_xsltComp = XsltProc.NewXsltCompiler()); }
        }
        static readonly Dictionary<string, XsltTransformer> Xslts = new Dictionary<string,XsltTransformer>();

        public static XsltTransformer LoadXslt(string name)
        {
            if (name == null) throw new ArgumentNullException("name");

            if (Xslts.ContainsKey(name)) return Xslts[name];
            var x = XsltComp.Compile(new StringReader(XsltStylesheets.ResourceManager.GetString(name))).Load();
            Xslts.Add(name, x);
            return x;
        }



        public  static XdmNode BuildDocument(System.Xml.XmlReader xmlReader)
        {
            return XsltProc.NewDocumentBuilder().Build(xmlReader);
        }
    }
}
